const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 检测机构-- 查询
router.post("/info", (req, res) => {
    const { name, pageConfig ,doName} = req.body;
    const { page, pageSize } = pageConfig
    let sql = name === undefined ? "select * from sgs order by Id limit ?,?;select * from sgs"
        : "select * from sgs where sgsName=?"
    const data = name === undefined ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] : [name]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            name === undefined ?
                res.send({
                    data: result[0],
                    total: result[1].length,
                    msg: "获取信息成功",
                    status: 200
                }) :
                res.send({
                    data: result,
                    total: result.length,
                    msg: "获取信息成功",
                    status: 200
                })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"检测机构查询",time.getData()])
        }
    })
})

// 检测机构-- 添加
router.post("/add", (req, res) => {
    const {sgsName,sgsLeader,sgsPhone,sgsPlace,sgsConStart,sgsConEnd,doName } = req.body
    let sql = `insert into sgs(sgsName,sgsLeader,sgsPhone,sgsPlace,sgsConStart,sgsConEnd) values(?,?,?,?,?,?)`
    const data = [sgsName,sgsLeader,sgsPhone,sgsPlace,sgsConStart,sgsConEnd]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"检测机构添加",time.getData()])
        }
    })
})

// 检测机构-- 编辑
router.post("/edit", (req, res) => {
    const {sgsName,sgsLeader,sgsPhone,sgsPlace,sgsConStart,sgsConEnd,Id,doName } = req.body
    let sql = `update sgs set sgsName=?,sgsLeader=?,sgsPhone=?,sgsPlace=?,sgsConStart=?,sgsConEnd=? where Id=?`
    const data = [sgsName,sgsLeader,sgsPhone,sgsPlace,sgsConStart,sgsConEnd,Id ]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"检测机构编辑",time.getData()])
        }
    })
})

// 检测机构-- 删除
router.post("/delete", (req, res) => {
    const { Id,doName } = req.body
    let sql = "delete from sgs where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"检测机构删除",time.getData()])
        }
    })
})

module.exports = router